#!/usr/bin/env node
const commander = require("commander");
const inquirer = require("inquirer");
const path = require('path');
const fs = require('fs')
const { dirname } = require('path');

commander.command('changeName [old] [new]').action((oldName, newName) => {
    //获取当前目录
    const files = fs.readdirSync(__dirname);
    //判断当前目录
    const haveFile = files.find(item => item === oldName)
    if (haveFile) {
        fs.renameSync(oldName, newName)
    } else {
        //进行模糊匹配
        const filterList = files.filter(item => item.includes(oldName))
        let ind = 0;
        filterList.forEach(item => {
            ind += 1
            fs.renameSync(item,`${newName.split('.')[0]}(${ind}).${newName.split('.')[1]}`)

        })
    }
})
// 删除文件或文件夹
commander.command('deleteFile [fileName]').action(name=>{
    // 1. 判断name是否存在
    // 2. 判断是文件还是文件夹
    // 3. 文件直接删除  文件夹遍历所有子文件并删除
    const isHave = fs.existsSync(name);
    if(isHave) {
        delFile(__dirname, name)
        console.log("删除成功");
    } else {
        console.log('没有这个文件')
    }
})
function delFile(dirName, name) {
    const url = path.join(dirName,name)
    const stat = fs.statSync(url)
    if(stat.isFile()) {
        fs.unlinkSync(url)  
    } else { 
        const files = fs.readdirSync(url)
        files.forEach(item => {
            delFile(url,item)
        })
        fs.rmdirSync(url)
    } 
}
commander.command('showFile [env]').action(option=>{
    if(option){
        readFile(__dirname,option)
    }else{
        console.log(('请你输入文件名'));
    }
})
function readFile(dirname,name){
    const url =path.join(dirname,name)
    fs.stat(url,())
}
console.log(111);
commander.parse(process.argv)
